<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Urban_ui extends CI_Controller {

	public function index()
	{
		$this->lang->load('ui');
		$this->load->view('base');
		
	}
	
	public function login() {
		$result = $this->user->validate(
			$this->input->post('email'),
			$this->input->post('password'));
			
		if ($result)
				$this->session->set_userdata(array('user_id'=>$result));
			else
				$this->session->unset_userdata('user_id');
			
		redirect('/');
		
	}
	
	public function logout() {
		$this->session->unset_userdata('user_id');
		redirect('/');
	}
	
	public function panel($panel,$id=NULL,$id2=NULL) {
		$this->lang->load('ui');
		
		if (!$this->user->is_valid())
			error("You must login as a CMS user to access the UrbanCMS interface",'Access denied',401);
		
		$data['panel'] = $panel;
		
		try {
			switch($panel) {
				case 'users':
					$data['panel_data']['users'] = $this->user->get_all(); 
					break;
				case 'cats': 
					break;
				case 'objectstruct':
					$this->load->model('object');
					$this->load->model('field');
					$data['panel_data']['object'] = $this->object->get($id);
					$data['panel_data']['fields'] = $this->field->get_by_object($id); 
					break;
				case 'objectentries':
					$this->load->model('object');
					$this->load->model('field');
					$data['panel_data']['object'] = $this->object->get($id);
					$data['panel_data']['fields'] = $this->field->get_by_object($id);
					$data['panel_data']['entries'] = $this->object->get_entries($id);
				case 'objectnewentry':
					$this->load->model('language');
					$this->load->model('object');
					$this->load->model('field');
					$data['scripts']['colorpicker'] = TRUE;
					$data['scripts']['datepicker'] = TRUE;
					$data['panel_data']['languages'] =$this->language->get_all(); 
					$data['panel_data']['object'] = $this->object->get($id);
					$data['panel_data']['fields'] = $this->field->get_by_object($id);
					break;
				case 'objectentry':
					$this->load->helper('form');
					$this->load->helper('file');
					$this->load->model('language');
					$this->load->model('category');
					$this->load->model('object');
					$this->load->model('field');
					$data['scripts']['colorpicker'] = TRUE;
					$data['scripts']['datepicker'] = TRUE;
					$data['panel_data']['languages'] =$this->language->get_all(); 
					$data['panel_data']['categories'] = $this->category->get_all();
					$data['panel_data']['object'] = $this->object->get($id);
					$data['panel_data']['fields'] = $this->field->get_by_object($id);
					$data['panel_data']['entry'] = $this->object->get_entry($id,$id2,TRUE);
					break;
				case 'about':
					$data['panel_data'] = array(); 
					break;
				case 'objects': 
					$this->load->model('object');
					$data['panel_data']['objects'] = $this->object->get_all();
					break;
				case 'categories':
					$this->load->model('category');
					$this->load->model('language');
					$data['scripts']['colorpicker'] = TRUE;
					$data['panel_data']['categories'] = $this->category->get_all();
					$data['panel_data']['languages'] =$this->language->get_all(); 
					break;
				default:
					exit(); 
			};
		} catch (Exception $e) {
			$this->load->library('user_agent');
			error($e->getMessage(),'Failed to show CMS UI',403);
		}
		
		$this->load->view('base',$data);
	}

	
}

	

